R Markdown

Grabbing Static tiles

There are various styles, including streets, outdoors, light, dark, satellite, satellite stretes, navigation day, navigation night. https://docs.mapbox.com/api/maps/styles/

mapbox_token <- Sys.getenv("MAPBOX_PUBLIC_TOKEN")

ny_tracts <- tracts("NY",c("Kings","Bronx","Queens","Richmond","New York"),cb = TRUE)
## Retrieving data for the year 2021
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
NYC_Income <- get_acs(
  geography = "tract",
  variables = "B19013_001",
  state = "NY",
  county = c("Kings","Bronx","Queens","Richmond","New York"),
  geometry = TRUE,
  year = 2019
)
## Getting data from the 2015-2019 5-year ACS
## Warning: • You have not set a Census API key. Users without a key are limited to 500
## queries per day and may experience performance limitations.
## ℹ For best results, get a Census API key at http://api.census.gov/data/
## key_signup.html and then supply the key to the `census_api_key()` function to
## use it throughout your tidycensus session.
## This warning is displayed once per session.
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
NYC_Income_cleaned  <- NYC_Income %>% 
    drop_na()

NYC_Tiles <- get_static_tiles(
  location = ny_tracts,
  zoom = 9,
  style_id = "light-v9",
  username = "mapbox"
)
## Attribution is required if using Mapbox tiles on a map.
## Add the text '(c) Mapbox, (c) OpenStreetMap' to your map for proper attribution.
tm_shape(NYC_Tiles) + 
  tm_rgb() + 
  tm_shape(NYC_Income_cleaned) + 
  tm_polygons(col = "estimate", 
              alpha = 0.5, palette = "viridis", 
              title = "Median household income\n2015-2019 ACS",
              lwd = .3) + 
  tm_layout(legend.outside = TRUE) + 
  tm_credits("Basemap © Mapbox, © OpenStreetMap", position = c("RIGHT", "BOTTOM"))

Here’s how you can get vector tilesets

vector_extract <- get_vector_tiles(
  tileset_id = "mapbox.mapbox-streets-v8",
  location = c(-73.99405, 40.72033),
  zoom = 15
)

names(vector_extract)
## [1] "building"           "landuse"            "place_label"       
## [4] "poi_label"          "road"               "structure"         
## [7] "transit_stop_label" "water"
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(vector_extract$landuse$polygons) + 
  tm_polygons(col = "type", alpha = .4) 
tm_shape(vector_extract$building$polygons) + 
  tm_polygons(col = "type", alpha = .4) 

Plotting with a Mapbox Basemap

# bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
# pal <- colorBin("YlOrRd", domain = vector_extract$building$polygons, bins = bins)
factpal <- colorFactor(topo.colors(16), vector_extract$building$polygons$type)

leaflet() %>%
  addMapboxTiles(style_id = "dark-v11",
                 username = "mapbox") %>%
  setView(lng = -73.99405, #c(-73.99405, 40.72033)
          lat = 40.72033,
          zoom = 16) %>% 
    addPolygons(data = vector_extract$building$polygons,
                popup = vector_extract$building$polygons$type,
                color = "white",
                fillColor = ~factpal(type),
                weight = 2) %>% 
    addLegend(pal = factpal,value =  vector_extract$building$polygons$type,opacity = 1)

Isochrone (service area) analysis

isochrones <- mb_isochrone("Chinatown, NYC", 
                           time = c(4, 8, 12),
                           profile = "cycling") 
mapdeck(style = mapdeck_style("dark")) %>%

  add_polygon(data = isochrones, 
              fill_colour = "time",
              fill_opacity = 0.5,
              legend = TRUE) 
## Registered S3 method overwritten by 'jsonify':
##   method     from    
##   print.json jsonlite